package com.yanyilu.qiubite.service;

import com.yanyilu.qiubite.entity.AdminUser;
import com.yanyilu.qiubite.entity.Spot;
import com.yanyilu.qiubite.jpa.AdminUserJpa;
import com.yanyilu.qiubite.jpa.StoryJpa;
import com.yanyilu.qiubite.util.PageUtil;
import com.yanyilu.qiubite.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class AdminUserService {
    @Autowired
    AdminUserJpa adminUserJpa;

    @Autowired
    StoryJpa storyJpa;
    public AdminUser login(AdminUser adminUser) {

        Example<AdminUser> example = Example.of(adminUser);
        Optional<AdminUser> one = adminUserJpa.findOne(example);
        if(one.isPresent()){
            return one.get();
        }else {
            return null;
        }
    }

    public Page<Object[]> findUsersBySpotId(Integer spotId, Integer page, Integer limit) {
        //封装分页
        Pageable pageable = PageUtil.pagelimit(page, limit);


        return adminUserJpa.findBySpotId(spotId,pageable);
    }

    public Page<AdminUser> findAll(Integer page, Integer limit) {
        //封装分页
        Pageable pageable = PageUtil.pagelimit(page, limit);

        return adminUserJpa.findAll(pageable);
    }

    public AdminUser findUserById(Integer userid) {
        Optional<AdminUser> optional = adminUserJpa.findById(userid);
        if(optional.isPresent()){
            return optional.get();
        }else{
            return null;
        }

    }

    /**
     * 超级管理员修改
     * @param adminuser
     */
    public void updateAdminUser(AdminUser adminuser) {

        Optional<AdminUser> optional = adminUserJpa.findById(adminuser.getId());
        if(optional.isPresent()){
            AdminUser adminUser1 = optional.get();
            Integer role = adminuser.getRole();
            if(null != role  && 0 != role){
                adminUser1.setRole(role);
            }
            adminUser1.setUserName(adminuser.getUserName());
            adminUser1.setPassword(adminuser.getPassword());
            adminUserJpa.save(adminUser1);
        }
    }


    /**
     * 非超级管理员修改
     * @param adminuser
     */
    public void updateAdminUser1(AdminUser adminuser) {
        Optional<AdminUser> optional = adminUserJpa.findById(adminuser.getId());
        if(optional.isPresent()){
            AdminUser adminUser1 = optional.get();
            adminUser1.setUserName(adminuser.getUserName());
            adminUser1.setPassword(adminuser.getPassword());
            adminUserJpa.save(adminUser1);
        }
    }

    //查重
    public boolean findByUserName(String userName,Integer userId) {
        System.out.println("111111   ="+adminUserJpa.countByUserName(userName,userId));
        int  count = adminUserJpa.countByUserName(userName,userId);
        return count == 1;
    }

    public boolean addUser(AdminUser adminUser, Integer userid) {
        if(null == adminUser.getRole()){
            Integer spotId = adminUserJpa.findSpotIdByUserId(userid);
            adminUser.setSpotId(spotId);
            adminUser.setRole(2);
            adminUserJpa.save(adminUser);
            return true;
        }
        return false;
    }

    public void addUserFromAdmin(AdminUser adminUser) {
        if(null != adminUser.getRole() && adminUser.getRole() == 0){
            adminUser.setRole(1);
        }
        adminUserJpa.save(adminUser);
    }

    public Integer findSpotIdByUserId(Integer userid) {
        return adminUserJpa.findSpotIdByUserId(userid);
    }

    public Spot findSpotUserId(Integer userid) {
        return adminUserJpa.findSpotByUserId(userid);
    }

    public Integer findSpotIdUserId(Integer userid) {
        return adminUserJpa.findSpotIdByUserId(userid);
    }

    public List<Integer> findStoryIdByUserId(Integer userid) {
        Integer spotId = adminUserJpa.findSpotIdByUserId(userid);
        return storyJpa.findStoryBySpotId(spotId);
    }

    public Page<Object[]> findAllUser(Integer page, Integer limit) {
        Pageable pageable = PageUtil.pagelimit(page, limit);
        return adminUserJpa.findAllUser(pageable);
    }
}
